<template>
	<div class="u-field" :class="{'u-border-top': borderTop, 'u-border-bottom': borderBottom }">
		<div class="u-field-inner"
			:class="[type == 'textarea' ? 'u-textarea-inner' : '', 'u-label-postion-' + labelPosition]">
			<div class="u-label" :class="[required ? 'u-required' : '']" :style="{
				justifyContent: justifyContent, 
				flex: labelPosition == 'left' ? `0 0 ${labelWidth}px` : '1'
			}">
				<div class="u-icon-wrap" v-if="icon">
					<u-icon size="20" :custom-style="iconStyle" :name="icon" :color="iconColor" class="u-icon"></u-icon>
				</div>
				<slot name="icon"></slot>
				<span class="u-label-text" :class="[this.$slots.icon || icon ? 'u-label-left-gap' : '']">{{ label }}</span>
			</div>
			<div class="fild-body">
				<div class="u-flex-1 u-flex" :style="[inputWrapStyle]">
					<template v-if="$slots.default">
						<slot/>
					</template>
					<template v-else>
					<textarea v-if="type == 'textarea'" class="u-flex-1 u-textarea-class" :style="[fieldStyle]" :value="value"
						:placeholder="placeholder" :placeholderStyle="placeholderStyle" :disabled="disabled" :maxlength="inputMaxlength"
						:focus="focus" :autoHeight="autoHeight" :fixed="fixed" @input="onInput" @blur="onBlur" @focus="onFocus"
						@confirm="onConfirm" @tap="fieldClick" />
					<input
						v-else
						:style="[fieldStyle]"
						:type="type"
						class="u-flex-1 u-field__input-wrap uni-input-input"
						:value="value"
						:password="password || this.type === 'password'"
						:placeholder="placeholder"
						:placeholderStyle="placeholderStyle"
						:disabled="disabled"
						:maxlength="inputMaxlength"
						:focus="focus"
						:confirmType="confirmType"
						@focus="onFocus"
						@blur="onBlur"
						@input="onInput"
						@confirm="onConfirm"
						@tap="fieldClick"
					/>
					</template>
				</div>
				<u-icon :size="clearSize" v-if="clearable && value != '' && focused" name="close-circle-fill" color="#c0c4cc" class="u-clear-icon" @click="onClear"/>
				<div class="u-button-wrap"><slot name="right" /></div>
				<u-icon v-if="rightIcon" @click="rightIconClick" :name="rightIcon" color="#c0c4cc" :style="[rightIconStyle]" size="26" class="u-arror-right" />
			</div>
		</div>
		<div v-if="errorMessage !== false && errorMessage != ''" class="u-error-message" :style="{
			paddingLeft: labelWidth + 'px'
		}">{{ errorMessage }}</div>
	</div>
</template>

<script>

export default {
	name:"u-field",
	props: {
		icon: String,
		rightIcon: String,
		// arrowDirection: {
		// 	type: String,
		// 	default: 'right'
		// },
		required: Boolean,
		label: String,
		password: Boolean,
		clearable: {
			type: Boolean,
			default: true
		},
		// 左边标题的宽度单px位
		labelWidth: {
			type: [Number, String],
			default: 130
		},
		// 对齐方式，left|center|right
		labelAlign: {
			type: String,
			default: 'left'
		},
		inputAlign: {
			type: String,
			default: 'left'
		},
		iconColor: {
			type: String,
			default: '#606266'
		},
		autoHeight: {
			type: Boolean,
			default: true
		},
		errorMessage: {
			type: [String, Boolean],
			default: ''
		},
		placeholder: String,
		placeholderStyle: String,
		focus: Boolean,
		fixed: Boolean,
		value: [Number, String],
		type: {
			type: String,
			default: 'text'
		},
		disabled: {
			type: Boolean,
			default: false
		},
		maxlength: {
			type: [Number, String],
			default: 140
		},
		confirmType: {
			type: String,
			default: 'done'
		},
		// lable的位置，可选为 left-左边，top-上边
		labelPosition: {
			type: String,
			default: 'left'
		},
		// 输入框的自定义样式
		fieldStyle: {
			type: Object,
			default() {
				return {}
			}
		},
		// 清除按钮的大小
		clearSize: {
			type: [Number, String],
			default: 26
		},
		// lable左边的图标样式，对象形式
		iconStyle: {
			type: Object,
			default() {
				return {}
			}
		},
		// 是否显示上边框
		borderTop: {
			type: Boolean,
			default: false
		},
		// 是否显示下边框
		borderBottom: {
			type: Boolean,
			default: true
		},
		// 是否自动去除两端的空格
		trim: {
			type: Boolean,
			default: true
		}
	},
	data() {
		return {
			focused: false,
			itemIndex: 0,
		};
	},
	computed: {
		inputWrapStyle() {
			let style = {};
			style.textAlign = this.inputAlign;
			// 判断lable的位置，如果是left的话，让input左边两边有间隙
			if(this.labelPosition == 'left') {
				style.margin = `0 8px`;
			} else {
				// 如果lable是top的，input的左边就没必要有间隙了
				style.marginRight = `8px`;
			}
			return style;
		},
		rightIconStyle() {
			let style = {};
			if (this.arrowDirection == 'top') style.transform = 'roate(-90deg)';
			if (this.arrowDirection == 'bottom') style.transform = 'roate(90deg)';
			else style.transform = 'roate(0deg)';
			return style;
		},
		labelStyle() {
			let style = {};
			if(this.labelAlign == 'left') style.justifyContent = 'flext-start';
			if(this.labelAlign == 'center') style.justifyContent = 'center';
			if(this.labelAlign == 'right') style.justifyContent = 'flext-end';
			return style;
		},
		// uni不支持在computed中写style.justifyContent = 'center'的形式，故用此方法
		justifyContent() {
			if(this.labelAlign == 'left') return 'flex-start';
			if(this.labelAlign == 'center') return 'center';
			if(this.labelAlign == 'right') return 'flex-end';
		},
		// 因为uniapp的input组件的maxlength组件必须要数值，这里转为数值，给用户可以传入字符串数值
		inputMaxlength() {
			return Number(this.maxlength)
		},
		// label的位置
		fieldInnerStyle() {
			let style = {};
			if(this.labelPosition == 'left') {
				style.flexDirection = 'row';
			} else {
				style.flexDirection = 'column';
			}
			
			return style;
		}
	},
	methods: {
		onInput(event) {
			let value = event.detail.value;
			// 判断是否去除空格
			// if(this.trim) value = this.$u.trim(value);
			this.$emit('input', value);
		},
		onFocus(event) {
			this.focused = true;
			this.$emit('focus', event);
		},
		onBlur(event) {
			// 最开始使用的是监听图标@touchstart事件，自从hx2.8.4后，此方法在微信小程序出错
			// 这里改为监听点击事件，手点击清除图标时，同时也发生了@blur事件，导致图标消失而无法点击，这里做一个延时
			setTimeout(() => {
				this.focused = false;
			}, 100)
			this.$emit('blur', event);
		},
		onConfirm(e) {
			this.$emit('confirm', e.detail.value);
		},
		onClear(event) {
			this.$emit('input', '');
		},
		rightIconClick() {
			this.$emit('right-icon-click');
			this.$emit('click');
		},
		fieldClick() {
			this.$emit('click');
		}
	}
};
</script>

<style lang="scss" scoped>
// @import "../../libs/css/style.components.scss";
	
.u-field {
	font-size: 20px;
	padding: 15px 20px;
	text-align: left;
	position: relative;
	width: 100%;
	cursor: pointer;
	// color: $u-main-color;
}
.u-flex-1{
	flex: 1;
}

.u-field-inner {
display: flex;	
	align-items: center;
}

.u-textarea-inner {
	align-items: flex-start;
}

.u-textarea-class {
	min-height: 96px;
	width: auto;
	font-size: 20px;
}

.fild-body {
display: flex;	
	flex: 1;
	align-items: center;
}

.u-arror-right {
	margin-left: 8px;
}

.u-label-text {
	/* #ifndef APP-NVUE */
	display: inline-flex;		
	/* #endif */
}

.u-label-left-gap {
	margin-left: 6px;
}

.u-label-postion-top {
	flex-direction: column;
	align-items: flex-start;
}

.u-label {
	width: 130px;
	flex: 1 1 130px;
	text-align: left;
	position: relative;
display: flex;	
	align-items: center;
}

.u-required::before {
	content: '*';
	position: absolute;
	left: -16px;
	font-size: 14px;
	// color: $u-type-error;
	height: 9px;
	line-height: 1;
}

.u-field__input-wrap {
	position: relative;
	overflow: hidden;
	font-size: 28px;
	height: 48px;
	flex: 1;
	width: auto;
}

.u-clear-icon {
display: flex;	
	align-items: center;
}

.u-error-message {
	// color: $u-type-error;
	font-size: 26px;
	text-align: left;
}

.placeholder-style {
	color: rgb(150, 151, 153);
}

.u-input-class {
	font-size: 20px;
}

.u-button-wrap {
	margin-left: 8px;
}
.uni-input-input:disabled {
    -webkit-text-fill-color: currentcolor;
}

.uni-input-input {
    position: relative;
    display: block;
    height: 100%;
    background: none;
    color: inherit;
    opacity: 1;
    font: inherit;
    line-height: inherit;
    letter-spacing: inherit;
    text-align: inherit;
    text-indent: inherit;
    text-transform: inherit;
    text-shadow: inherit;
    outline: none;
    border: none;
    padding: 0;
    margin: 0;
    text-decoration: inherit;
	cursor: pointer;
    width: 100%;
}
.u-border-bottom:after {
    content: ' ';
    position: absolute;
    left: 0;
    top: 0;
    pointer-events: none;
    box-sizing: border-box;
    transform-origin: 0 0;
    width: 199.8%;
    height: 199.7%;
    transform: scale(0.5, 0.5);
    border: 0 solid #e4e7ed;
    border-bottom-width: 1px;
    z-index: 2;
}
</style>